package service.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;

import persistence.dao.TdfkfaExpertListDAO;
import persistence.model.TdfkfaExpertList;
import persistence.model.TdfkfaExpertListExample;
import persistence.model.TdfkfaExpertListExample.Criteria;
import presentation.extendmodel.SimpleExpertModel;
import presentation.extendmodel.TdfkfaExpertListEx;
import presentation.queryvo.ExpertListQueryVO;
import service.ExpertService;
import util.StringUtil;
import constant.ServiceConstants;

public class ExpertServiceImpl implements ExpertService {

    private TdfkfaExpertListDAO tdfkfaExpertListDAO;

    public List<TdfkfaExpertListEx> getExpertList(ExpertListQueryVO queryVO, int pageIndex, int pageSize) {

        TdfkfaExpertListExample example = new TdfkfaExpertListExample();
        Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(queryVO.getName())) {
            criteria.andExpertnameLike("%".concat(queryVO.getName()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getType())) {
            criteria.andTypeEqualTo(new Short(queryVO.getType()));
        }
        example.setOrderByClause("ExpertID desc");
        
        List<TdfkfaExpertListEx> expertList = tdfkfaExpertListDAO.selectExpertByExample(example, pageIndex, pageSize);
        return expertList;
    }

    /**
     * 取得所有专家数量
     */
    public int countExpert(ExpertListQueryVO queryVO) {

        TdfkfaExpertListExample example = new TdfkfaExpertListExample();
        Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(queryVO.getName())) {
            criteria.andExpertnameLike("%".concat(queryVO.getName()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getType())) {
            criteria.andTypeEqualTo(new Short(queryVO.getType()));
        }
        
        int count = tdfkfaExpertListDAO.countByExample(example);
        return count;
    }

    /**
     * 取得在审专家数据
     * 
     * @return
     */
    public List<TdfkfaExpertListEx> getJudgingExpertList(ExpertListQueryVO queryVO, int pageIndex, int pageSize) {

        TdfkfaExpertListExample example = new TdfkfaExpertListExample();
        Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(queryVO.getName())) {
            criteria.andExpertnameLike("%".concat(queryVO.getName()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getType())) {
            criteria.andTypeEqualTo(new Short(queryVO.getType()));
        }
        example.setOrderByClause("ExpertID desc");
        
        List<TdfkfaExpertListEx> list = tdfkfaExpertListDAO.selectJudgingExpertByExample(example, pageIndex, pageSize);
        return list;
    }
    
    /**
     * 取得在审专家数量
     */
    public int countJudgingExpert(ExpertListQueryVO queryVO) {

        TdfkfaExpertListExample example = new TdfkfaExpertListExample();
        Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(queryVO.getName())) {
            criteria.andExpertnameLike("%".concat(queryVO.getName()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getType())) {
            criteria.andTypeEqualTo(new Short(queryVO.getType()));
        }
        
        int count = tdfkfaExpertListDAO.countJudgingExpertByExample(example);
        return count;
    }

    public int saveExpert(TdfkfaExpertList expert) {

    	try {
            tdfkfaExpertListDAO.insertExpert(expert);
    	} catch (DataAccessException e) {
    		return ServiceConstants.FAILURE;
    	}
    	
        return ServiceConstants.SUCCESS;
    }

    /**
     * 取得专家单位列表
     */
    public List<String> getExpertUnitList() {
        List<String> list = tdfkfaExpertListDAO.selectExpertUnitByExample(new TdfkfaExpertListExample());
        return list;
    }

    /**
     * 取得专家名列表
     */
    public List<String> getExpertNameList(String unit) {
        TdfkfaExpertListExample example = new TdfkfaExpertListExample();
        example.createCriteria().andUnitEqualTo(unit);
        List<String> list = tdfkfaExpertListDAO.selectExpertNameByExample(example);
        return list;
    }

    public List<TdfkfaExpertList> getExpertList(String unitName) {

        TdfkfaExpertListExample example = new TdfkfaExpertListExample();
        // example.createCriteria().andUnitEqualTo(unitName);
        List<TdfkfaExpertList> expertList = tdfkfaExpertListDAO.selectByExampleWithoutBLOBs(example);
        return expertList;
    }
    
    public void deleteExpert(Integer[] ids) {
        for (Integer id : ids) {
            tdfkfaExpertListDAO.deleteByPrimaryKey(id);
        }
    }
    
    public int updateExpert(TdfkfaExpertList expert) {
        int ret = tdfkfaExpertListDAO.updateByPrimaryKeySelective(expert);
        return ret;
    }
    
    public TdfkfaExpertList getExpert(Integer expertId) {
        return tdfkfaExpertListDAO.selectByPrimaryKey(expertId);
    }

    public List<SimpleExpertModel> getSimpleExpertList(ExpertListQueryVO queryVO) {
        TdfkfaExpertListExample example = new TdfkfaExpertListExample();
        Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(queryVO.getCity())) {
            criteria.andCityEqualTo(new Integer(queryVO.getCity()));
        }
        if (!StringUtil.isEmpty(queryVO.getType())) {
            criteria.andTypeEqualTo(new Short(queryVO.getType()));
        }
        List<SimpleExpertModel> expertList = tdfkfaExpertListDAO.selectSimpleExpertByExample(example);
        return expertList;
    }
    
    public void setTdfkfaExpertListDAO(TdfkfaExpertListDAO tdfkfaExpertListDAO) {
        this.tdfkfaExpertListDAO = tdfkfaExpertListDAO;
    }

	public TdfkfaExpertList getExpertByUsername(String username) {
		TdfkfaExpertListExample example = new TdfkfaExpertListExample();
		example.createCriteria().andUsernameEqualTo(username);
		List<TdfkfaExpertList> list = tdfkfaExpertListDAO.selectByExampleWithoutBLOBs(example);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

}
